package com.edu.dao.impl;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.edu.bean.Carimg;
import com.edu.bean.Comment;
import com.edu.dao.BaseDao;
import com.edu.dao.CarimgDao;

public class CarimgDaoImpl implements CarimgDao {

	@Override
	public List<Carimg> selectCarimgAll(int pageNum, int pageSize) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet res = null;
		List<Carimg> carimg = new ArrayList();
		String sql = "SELECT * FROM carimg LIMIT ?,?";
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			ps.setInt(1, (pageNum-1)*pageSize);
			ps.setInt(2, pageSize);
			res = ps.executeQuery();
			while(res.next()) {
				Carimg img = new Carimg();
				img.setCarimgid(res.getInt("carimgid"));
				img.setCarid(res.getInt("carid"));
				img.setCarimgkv(res.getString("carimgkv"));
				img.setCarimgivi(res.getString("carimgivi"));
				img.setFrontFender(res.getString("frontFender"));
				img.setCarImgAc(res.getString("carImgAc"));
				img.setCarImgMph(res.getString("carImgMph"));
				img.setCarImgAudio(res.getString("carImgAudio"));
				img.setCarImgSeat(res.getString("carImgSeat"));
				img.setCarImgSunroof(res.getString("carImgSunroof"));
				carimg.add(img);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(con, ps, res);
		}
		
		return carimg;
	}

	@Override
	public int selectCarimgtotal(Carimg carimg) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet res = null;
		int result = 0;
		String sql = "SELECT count(1) as total FROM carimg WHERE 1=1 ";
		List<Object> obj = new ArrayList<Object>();
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			res = ps.executeQuery();
			if(res.next()) {
				result = res.getInt("total");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(con, ps, res);
		}
		
		return result;
	}

	@Override
	public int deleteCarimgById(int carimgid) {
		Connection con = null;
		PreparedStatement ps = null;
		int result = 0;
		String sql = "DELETE FROM carimg WHERE carImgId = ?";
		List<Object> obj = new ArrayList<Object>();
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			ps.setInt(1, carimgid);
			result = ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(con, ps, null);
		}
		
		return result;
	}

	@Override
	public int updateCarimg(Carimg carimg) {
		Connection con = null;
		PreparedStatement ps = null;
		int result = 0;
		String sql = "UPDATE carimg set carId = ?,carImgKV = ?,carImgIVI = ?,frontFender = ?,carImgAc= ?,carImgMph= ?,carImgAudio= ?,carImgSeat= ?,carImgSunroof= ? WHERE carImgId = ? ";
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			ps.setInt(1, carimg.getCarid());
			ps.setString(2, carimg.getCarimgkv());
			ps.setString(3, carimg.getCarimgivi());
			ps.setString(4, carimg.getFrontFender());
			ps.setString(5, carimg.getCarImgAc());
			ps.setString(6, carimg.getCarImgMph());
			ps.setString(7, carimg.getCarImgAudio());
			ps.setString(8, carimg.getCarImgSeat());
			ps.setString(9, carimg.getCarImgSunroof());
			ps.setInt(10, carimg.getCarimgid());
			System.out.println(ps);
			result = ps.executeUpdate();
		} catch (SQLException e) {			
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(con, ps, null);
		}
		return result;
	}

	@Override
	public Carimg selectCommentById(int carimgid) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet res = null;
		Carimg img=null;
		String sql = "SELECT * FROM carimg WHERE carImgId = ?";
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			ps.setInt(1,carimgid);
			res = ps.executeQuery();
			if(res.next()) {
				img = new Carimg();
				img.setCarimgid(res.getInt("carimgid"));
				img.setCarid(res.getInt("carid"));
				img.setCarimgkv(res.getString("carimgkv"));
				img.setCarimgivi(res.getString("carimgivi"));
				img.setFrontFender(res.getString("frontFender"));
				img.setCarImgAc(res.getString("carImgAc"));
				img.setCarImgMph(res.getString("carImgMph"));
				img.setCarImgAudio(res.getString("carImgAudio"));
				img.setCarImgSeat(res.getString("carImgSeat"));
				img.setCarImgSunroof(res.getString("carImgSunroof"));
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(con, ps, res);
		}
		
		return img;
	}

	@Override
	public int addCarimg(Carimg carimg) {
		Connection con = null;
		PreparedStatement ps = null;
		int result = 0;
		String sql = "INSERT INTO carimg(carId,carImgKV,carImgIVI,frontFender,carImgAc,carImgMph,carImgAudio,carImgSeat,carImgSunroof) VALUES (?,?,?,?,?,?,?,?,?)";
		try {
			con = BaseDao.getCon();
			ps.setInt(1, carimg.getCarid());
			ps.setString(2, carimg.getCarimgkv());
			ps.setString(3, carimg.getCarimgivi());
			ps.setString(4, carimg.getFrontFender());
			ps.setString(5, carimg.getCarImgAc());
			ps.setString(6, carimg.getCarImgMph());
			ps.setString(7, carimg.getCarImgAudio());
			ps.setString(8, carimg.getCarImgSeat());
			ps.setString(9, carimg.getCarImgSunroof());
			result = ps.executeUpdate();
			System.out.println(ps);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(con, ps, null);
		}
		return result;
	}

}
